using Senparc.Weixin.QY.Helpers;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
namespace Senparc.Weixin.QY.TenPayLib
{
	public class ResponseHandler
	{
		private string Key;
		private string Appkey;
		private Hashtable XmlMap;
		protected Hashtable Parameters;
		private string DebugInfo;
		protected string Content;
		private string Charset = "gb2312";
		protected HttpContext HttpContext;
		public virtual void Init()
		{
		}
		public ResponseHandler(HttpContext httpContext)
		{
			this.Parameters = new Hashtable();
			this.XmlMap = new Hashtable();
			this.HttpContext = (httpContext ?? HttpContext.Current);
			NameValueCollection nameValueCollection;
			if (this.HttpContext.Request.HttpMethod == "POST")
			{
				nameValueCollection = this.HttpContext.Request.Form;
				foreach (string text in nameValueCollection)
				{
					string parameterValue = nameValueCollection[text];
					this.SetParameter(text, parameterValue);
				}
			}
			nameValueCollection = this.HttpContext.Request.QueryString;
			foreach (string text2 in nameValueCollection)
			{
				string parameterValue2 = nameValueCollection[text2];
				this.SetParameter(text2, parameterValue2);
			}
			if (this.HttpContext.Request.InputStream.Length > 0L)
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(this.HttpContext.Request.InputStream);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("xml");
				XmlNodeList childNodes = xmlNode.ChildNodes;
				foreach (XmlNode xmlNode2 in childNodes)
				{
					this.XmlMap.Add(xmlNode2.Name, xmlNode2.InnerText);
					this.SetParameter(xmlNode2.Name, xmlNode2.InnerText);
				}
			}
		}
		public string GetKey()
		{
			return this.Key;
		}
		public void SetKey(string key)
		{
			this.Key = key;
		}
		public string GetParameter(string parameter)
		{
			string text = (string)this.Parameters[parameter];
			if (text != null)
			{
				return text;
			}
			return "";
		}
		public void SetParameter(string parameter, string parameterValue)
		{
			if (parameter != null && parameter != "")
			{
				if (this.Parameters.Contains(parameter))
				{
					this.Parameters.Remove(parameter);
				}
				this.Parameters.Add(parameter, parameterValue);
			}
		}
		public virtual bool IsTenpaySign()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "key".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + text2 + "&");
				}
			}
			stringBuilder.Append("key=" + this.GetKey());
			string text3 = MD5UtilHelper.GetMD5(stringBuilder.ToString(), this.GetCharset()).ToLower();
			this.SetDebugInfo(stringBuilder.ToString() + " &sign=" + text3);
			return this.GetParameter("sign").ToLower().Equals(text3);
		}
		public string GetDebugInfo()
		{
			return this.DebugInfo;
		}
		protected void SetDebugInfo(string debugInfo)
		{
			this.DebugInfo = debugInfo;
		}
		protected virtual string GetCharset()
		{
			return this.HttpContext.Request.ContentEncoding.BodyName;
		}
		public string ParseXML()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<xml>");
			foreach (string text in this.Parameters.Keys)
			{
				string text2 = (string)this.Parameters[text];
				if (Regex.IsMatch(text2, "^[0-9.]$"))
				{
					stringBuilder.Append(string.Concat(new string[]
					{
						"<",
						text,
						">",
						text2,
						"</",
						text,
						">"
					}));
				}
				else
				{
					stringBuilder.Append(string.Concat(new string[]
					{
						"<",
						text,
						"><![CDATA[",
						text2,
						"]]></",
						text,
						">"
					}));
				}
			}
			stringBuilder.Append("</xml>");
			return stringBuilder.ToString();
		}
	}
}
